package itsm.isperp.module.controller.slm;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppRole;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.entity.slm.SlmAlarmObject;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.db.DbEntityFieldService;
import itsm.isperp.module.service.slm.SlmAlarmObjectService;
import itsm.isperp.module.service.slm.SlmAlarmSettingService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("slm/alarmObject")
public class SlmAlarmObjectController extends BaseController<SlmAlarmObject> {

	/**
	 * 
	 */
	@Autowired
	protected SlmAlarmObjectService slmAlarmObjectService;

	@Override
	public BaseService<SlmAlarmObject> getService() {
		return slmAlarmObjectService;
	}

	@Override
	public String getPageTitle() {
		return "告警对象";
	}

	@Autowired
	protected AppRoleService appRoleService;

	@Autowired
	protected DbEntityFieldService dbEntityFieldService;

	@Autowired
	SlmAlarmSettingService slmAlarmSettingService;

	@Override
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<SlmAlarmObject> list(JqGridRequest request,
			HttpServletRequest req) {

		String bpmNodeSettingId = req.getParameter("relationId");

		return this
				.getService()
				.findAll(
						request.isPageable(false).addFilterRule(
								"relationId", JqGridFilterRuleOp.eq,
								bpmNodeSettingId)).toPage();
	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		ModelAndView mv = this.getModelAndView();
		SlmAlarmObject model = null;
		String relationId = req.getParameter("relationId");

		mv.addObject("all", req.getParameter("all"));

		if (StringUtils.isEmpty(id)) {
			model = new SlmAlarmObject();
			model.setRelationId(relationId);
		} else {
			model = this.getService().findOne(id);
			relationId = model.getRelationId();
		}

		List<AppRole> roles = appRoleService.findAllRole("id", "name");

		// 先获取
		DbEntity dbEntity = slmAlarmSettingService.findDbEntityById(relationId);
		// 获取表单中所有的字段
		List<DbEntityField> fields = dbEntityFieldService.findAll(
				new JqGridRequest()
						.isPageable(false)
						.setSelectColumns("id,name,column_name")
						.addFilterRule("db_entity_id", JqGridFilterRuleOp.eq,
								dbEntity.getId())).getContent();

		mv.addObject("roles", roles);

		mv.addObject("fields", fields);

		mv.addObject("model", model);
		return mv;
	}

}
